package com.neko.seed.util;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Set;
@Slf4j
public class HttpUtil {

    private static  OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。

    public static String get(String url,Map param){
        return get_(url, param, null);
    }
    private static String get_(String url, Map param, String Authorization){
        try {

//            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
            if(param != null){
                Set<String> keys = param.keySet();
                url += "?";
                for(String key: keys){
                    url = url + key +"=" + param.get(key);
                }
            }
            Request request = null;
            if(StringUtils.isNotEmpty(Authorization)){
                request = new Request.Builder()
                        .url(url)//请求接口。如果需要传参拼接到接口后面。
                        .header("Authorization",Authorization)
                        .build();//创建Request 对象
            }else{
                request = new Request.Builder()
                        .url(url)//请求接口。如果需要传参拼接到接口后面。
                        .build();//创建Request 对象
            }
            Response response = null;
            response = client.newCall(request).execute();//得到Response 对象
            if (response.isSuccessful()) {
                ResponseBody body = response.body();
                String result = body.string();
                log.info(result);
                return result;
            }
        } catch (Exception e) {
            log.error(e.toString());
            return null;
        }
        return  null;
    }
    public static String post(String url,Map param){
        return post_(url, param, null);
    }

    private static String post_(String url,Map param,String authorization){
        try {

//            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
            FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
            if(param != null){
                Set<String> keys = param.keySet();
                for(String key: keys){
                    formBody.add(key,param.get(key).toString());
                }
            }
            Request request;
            if(authorization == null){
                request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .post(formBody.build())//传递请求体
                        .build();
            }else{
                request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .header("Authorization",authorization)
                        .post(formBody.build())//传递请求体
                        .build();
            }
            Response response = null;
            response = client.newCall(request).execute();//得到Response 对象
            if (response.isSuccessful()) {
                ResponseBody body = response.body();
                String result = body.string();
                log.info(result);
                return result;
            }
        } catch (Exception e) {
            log.error(e.toString());
            return null;
        }
        return null;
    }

    public static  String postJson(String url,Map param) {
        return postJson(url, param, null);

    }
    public static  String postJson(String url,String jsonBody) {
        return postJson(url, jsonBody, null);

    }
    public static  String postJson(String url,Map param,String authorization) {
        String json = Constant.gson.toJson(param);
        return postJson(url, json, authorization);
    }
    public static String postJson(String url,String jsonBody,String authorization) {
        try {
            String json = jsonBody;
            MediaType jSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jSON, json);
            Request request =null;
            if (!StringUtils.isEmpty(authorization)) {
                request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .header("Authorization",authorization)
                        .build();
            } else {
                request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
            }
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                ResponseBody body1 = response.body();
                String result = body1.string();
                log.info(result);
                return result;
            }
        } catch (Exception e) {
            log.error(e.toString());
            return null;
        }
        return null;
    }
}
